package com.light.applet.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.wltea.analyzer.cfg.Configuration;
import org.wltea.analyzer.cfg.DefaultConfig;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
import org.wltea.analyzer.dic.Dictionary;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 魏彪
 */
@Slf4j
public class KeywordUtils {

    /**
     * 每个词的最小长度
     */
    private static final int MIN_LEN = 2;

    /**
     * 提取词语，结果将按频率排序
     *
     * @param text 待提取的文本
     * @return 提取出的词
     */
    public static List<String> extract(String text) {
        Configuration cfg = DefaultConfig.getInstance();
        // 设置useSmart标志位 true-智能切分 false-细粒度切分
        cfg.setUseSmart(true);
        Dictionary.initial(cfg);
        StringReader reader = new StringReader(text);
        IKSegmenter ikSegmenter = new IKSegmenter(reader, cfg);
        Lexeme lex;
        List<String> result = new ArrayList<>();
        try {
            while ((lex = ikSegmenter.next()) != null) {
                String word = lex.getLexemeText();
                // 取出的词至少 #{MIN_LEN} 个字
                if (word.length() >= MIN_LEN) {
                    result.add(word);
                }
            }
            return result;
        } catch (Exception e) {
            log.info("分词失败 ===> {}", e.getMessage(), e.getCause());
        }
        return Collections.emptyList();
    }

}
